package io.babystar.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.CWOrderEntity;
import io.babystar.modules.business.entity.StudentEntity;
import io.babystar.modules.business.entity.StudentManageEntity;
import io.babystar.modules.business.service.ParentService;
import io.babystar.modules.business.service.StudentManageService;
import io.babystar.modules.business.service.StudentService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;


/**
 * 学员管理表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-05-25 13:44:26
 */
@RestController
@RequestMapping("generator/studentmanage")
public class StudentManageController {
    @Autowired
    private StudentManageService studentManageService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ParentService parentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:studentmanage:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = studentManageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:studentmanage:info")
    public R info(@PathVariable("id") Integer id){
		StudentManageEntity studentManage = studentManageService.getById(id);
        StudentEntity studentEntity = studentService.getById(studentManage.getStudentId());
        studentManage.setStudentName(studentEntity.getStudentName());
        studentManage.setConsultant(studentEntity.getConsultant());
        studentManage.setDueTime(studentEntity.getDueTime());

        return R.ok().put("studentManage", studentManage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:studentmanage:save")
    public R save(@RequestBody StudentManageEntity studentManage){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        studentManage.setCreator(userEntity.getUserId().toString());
        studentManage.setCreateAt(new Date());
        studentManageService.save(studentManage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:studentmanage:update")
    public R update(@RequestBody StudentManageEntity studentManage){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        studentManage.setUpdator(userEntity.getUserId().toString());
        studentManage.setUpdateAt(new Date());
        studentManageService.updateById(studentManage);
        //修改课程顾问
        if(null != studentManage.getConsultant()){
            StudentEntity studentEntity = new StudentEntity();
            studentEntity.setId(studentManage.getStudentId());
            studentEntity.setConsultant(studentManage.getConsultant());
            studentService.updateById(studentEntity);
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:studentmanage:delete")
    public R delete(@RequestBody Integer[] ids){
        //studentManageService.removeByIds(Arrays.asList(ids));
        for (Integer id : ids) {
            StudentManageEntity studentManageEntity = studentManageService.getById(id);
            studentManageEntity.setDelFlag(1);
            studentManageService.updateById(studentManageEntity);
        }
        return R.ok();
    }

    /**
     * 获取所有学员
     */
    @RequestMapping("/getStudentList")
    public R getStudentList() {
        List<StudentManageEntity> studentManageEntities = studentManageService.queryAll();
        return R.ok().put("list", studentManageEntities);
    }
    /**
     * 根据studentId获取
     */
    @RequestMapping("/getStudentManageId")
    public R getStudentManageId(Integer studentId) {
        StudentManageEntity StudentManage = studentManageService.getOne(new QueryWrapper<StudentManageEntity>().eq("student_id", studentId).eq("del_flag", 0));
        return R.ok().put("list", StudentManage);
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public R excelImport(@RequestParam(value="filename") MultipartFile file, HttpSession session){
        List<StudentManageEntity> list = null;
        try {
            list = studentManageService.add(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null != list && list.size() > 0){
            return R.ok().put("list",list);
        }else{
            return R.error("excel数据导入失败,请检查Excel表格数据是否正确。");
        }
        /*if(result > 0){
            return R.ok("excel文件数据导入成功！");
        }else{
            return R.error("excel数据导入失败！");
        }*/
    }
}
